package lk.icta.core.web.action;

import lk.icta.common.ELandRuntimeException;
import lk.icta.common.ErrorCodes;
import lk.icta.common.web.action.BaseAction;
import lk.icta.core.dao.AlienationRailwayDAO;
import lk.icta.core.dao.PaymentDAO;
import lk.icta.core.entity.AlienationRailway;
import lk.icta.core.entity.Payment;
import lk.icta.core.service.AlienationRailwayManagementService;
import lk.icta.core.service.RailwayPaymentManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: araaya
 * Date: 5/28/14
 * Time: 2:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class PaymentAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(PaymentAction.class);

    private RailwayPaymentManagementService railwayPaymentManagementService;
    private final AlienationRailwayDAO alienationRailwayDAO;
    private final PaymentDAO paymentDAO;

    private Payment payment;
    private long alId;
    private AlienationRailway alienation;


    public PaymentAction(RailwayPaymentManagementService railwayPaymentManagementService, AlienationRailwayDAO alienationRailwayDAO,PaymentDAO paymentDAO) {
        this.railwayPaymentManagementService = railwayPaymentManagementService;
        this.alienationRailwayDAO = alienationRailwayDAO;
        this.paymentDAO = paymentDAO;
    }

    public String pageLoad() {
        logger.debug("LandRailwayAction pageLoad");
        return SUCCESS;
    }


    public String paymentLoad() {
        logger.debug("payment Load ");
        alienation = alienationRailwayDAO.getAlienationById(alId, user);

        return SUCCESS;
    }

    public String confirmPaymentLoad() {
        logger.debug("confirm Payment Load ");

        payment=paymentDAO.getPaymentById(alId,user);
        alienation = alienationRailwayDAO.getAlienationById(payment.getAlienationId(), user);

        return SUCCESS;
    }

    public String makePayment() {
        try {
            logger.debug("make Payment");

            if (payment.getPaymentType() == Payment.PaymentType.CHEQUE.ordinal()) {
                payment.setPaymentDate(new Date());
                payment.setPaymentState(Payment.PaymentState.INVOICED);
            } else {

                payment.setPaymentDate(new Date());
                payment.setConfirmedTimeStamp(payment.getBillingDate());
                payment.setConfirmedUser(user);
                payment.setPaymentState(Payment.PaymentState.CONFIRMED);

            }


            railwayPaymentManagementService.addPayment(payment, alId, user);
            addActionMessage("Payment Detail Successfully Save");
            alienation = alienationRailwayDAO.getAlienationById(alId, user);

            //payment.setBillingDate(null);
            //payment.setRentalAmount(0);
            //payment.setTaxAmount(0);
            //payment.setReceiptReferenceNo("");

            return SUCCESS;

        } catch (ELandRuntimeException e) {
            switch (e.getErrorCode()) {
                case ErrorCodes.UNAUTHORIZED_ACCESS:
                    addActionError(getText("permission_denied.label"));
                    break;
                case ErrorCodes.INVALID_DATA:
                    addActionError(getText("invalid_data.label"));
                    break;
                case ErrorCodes.EXCEPTION_OCCURRED:
                    addActionError(getText("invalid_data.label"));
                    break;
            }

            return ERROR;

        } catch (Exception e) {
            logger.debug("makePayment error {} ", e.getMessage());
            e.printStackTrace();
            addActionError("Payment Detail Saving Error");
            return ERROR;
        }

    }

    public String confirmPayment() {
        try {

            railwayPaymentManagementService.confirmPayment(payment.getId(), user);
            addActionMessage("Payment Detail Confirm Success");

            payment=paymentDAO.getPaymentById(payment.getId(),user);
            alienation = alienationRailwayDAO.getAlienationById(payment.getAlienationId(), user);

            return SUCCESS;

        } catch (ELandRuntimeException e) {
            switch (e.getErrorCode()) {
                case ErrorCodes.UNAUTHORIZED_ACCESS:
                    addActionError(getText("permission_denied.label"));
                    break;
                case ErrorCodes.INVALID_DATA:
                    addActionError(getText("invalid_data.label"));
                    break;
                case ErrorCodes.EXCEPTION_OCCURRED:
                    addActionError(getText("invalid_data.label"));
                    break;
            }

            return ERROR;

        } catch (Exception e) {
            logger.debug("confirmPayment error {} ", e.getMessage());
            e.printStackTrace();
            addActionError("Payment Detail Confirming Error");
            return ERROR;
        }


    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public long getAlId() {
        return alId;
    }

    public void setAlId(long alId) {
        this.alId = alId;
    }

    public AlienationRailway getAlienation() {
        return alienation;
    }

    public void setAlienation(AlienationRailway alienation) {
        this.alienation = alienation;
    }
}
